import Vue from 'vue'
import Router from 'vue-router'
import user from './module/user'
import order from './module/order'
import invest from './module/invest'
import Food from './module/Food'
import staticmath from './module/static'
import Seller from './module/seller'


// vue全家桶开发
Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/register')
    },
    {
      path: '/forgetPass',
      name: 'forgetPass',
      component: () => import('@/views/forgetPass')
    },
    { path: '/login', name: 'login', component: () => import('@/views/login') },
    {
      path: '/',
      name: 'layout',
      redirect: '/welcome',
      component: () => import('@/views/layout'),
      children: [
        {
          path: '/welcome',
          name: 'welcome',
          component: () => import('@/views/welcome')
        },
        ...user,
        ...order,
        ...invest,
        ...Food,
        ...staticmath,
        ...Seller
      ]
    },
    {
      path: '*',
      redirect: '/404',
      name: '404',
      component: () => import('@/views/error/404')
    }
  ]
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.eleToken
  if (to.path == '/login' || to.path == '/register'||to.path=='/forgetPass') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
